package com.cn.server.module.httpHandler;

import com.cn.common.core.session.Session;
import com.cn.common.core.session.SessionImpl;
import com.cn.common.core.session.SessionManager;
import com.cn.common.module.ModuleId;
import com.cn.common.module.chat.ChatCmd;
import com.cn.common.module.chat.response.ChatResponse;
import com.cn.common.module.chat.response.ChatType;
import com.cn.server.module.player.dao.entity.Player;
import com.cn.server.module.player.service.PlayerService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.channel.*;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.channels.Channels;
import java.util.Set;

/**
 * WebSocket规范中定义了6种类型的桢，netty为其提供了具体的对应的POJO实现。
 * WebSocketFrame：所有桢的父类，所谓桢就是WebSocket服务在建立的时候，
 * 在通道中处理的数据类型。本列子中客户端和服务器之间处理的是文本信息。
 * 所以范型参数是TextWebSocketFrame。
 *
 * @Author: jia
 * @Descirption:
 * @Date: 2018/5/9 9:24
 * @Modified By:
 */


public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler {
    @Autowired
    private PlayerService playerService;
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
 /*       Object msg = new Object();
        ByteBuf byteBuf = (ByteBuf) msg;
        byteBuf.array();*/
        Channel channel = ctx.channel();
        TextWebSocketFrame frame = null;
        frame = (TextWebSocketFrame) msg;
        System.out.println("收到的消息是：" + frame.text());
        String text = "你好，浏览器！";
        TextWebSocketFrame rMsg = new TextWebSocketFrame(text);
        //ctx.channel().writeAndFlush(rMsg);
        //TODO
        //获取所有在线玩家
        Set<Long> onlinePlayers = SessionManager.getOnlinePlayers();

        //创建消息对象
        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatType(ChatType.PUBLIC_CHAT);
        chatResponse.setSendPlayerId(1);
        chatResponse.setSendPlayerName("jia");
        chatResponse.setMessage(frame.text());


        //发送消息
        for(long targetPlayerId : onlinePlayers){
            SessionManager.sendMessage(targetPlayerId, ModuleId.CHAT, ChatCmd.PUSHCHAT, chatResponse);
        }
        SessionImpl session = new SessionImpl(channel);
        playerService.registerAndLogin(session,"jia","jia");




    }

    //每个channel都有一个唯一的id值
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //打印出channel唯一值，asLongText方法是channel的id的全名
        System.out.println();
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常发生");
        ctx.close();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println();
        super.channelInactive(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //
        super.channelActive(ctx);
    }

}
